﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDoc.Controls.Device
{
    public partial class ucReportDownload : System.Web.UI.UserControl
    {
        private string strFormat = "Tổng số lượt tải ứng dụng trong tháng <span style='font-weight:bold'>{0}</span> là : <span style='font-weight:bold'>{1}</span>";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDateTime();
            }
        }

        private void BindDateTime()
        {
            DateTime dtime = DateTime.Now;
            txtMonth.Text = dtime.Month.ToString();
            txtYear.Text = dtime.Year.ToString();
            DateTime expiry = new DateTime(Convert.ToInt32(txtYear.Text), Convert.ToInt32(txtMonth.Text), 1);
            BindData(expiry);
        }

        private void BindData(DateTime dateTime)
        {
            DataTable dt = new DataTable();
            dt = BO.Data.Select_DevicesDownload_by_Month(dateTime);

            grdReportDownload.DataSource = dt;
            grdReportDownload.DataBind();

            //repDownload.DataSource = dt;
            //repDownload.DataBind();

            ltrDonwloadCount.Text = String.Format(strFormat, dateTime.Month, dt.Rows.Count);

            //if(dt==null || dt.Rows.Count==0)
            //{
            //    trResult.Visible = true;
            //}
            //else
            //{
            //    trResult.Visible = false;
            //}
        }

        protected void btView_Click(object sender, EventArgs e)
        {
            if (txtYear.Text.Length < 4)
            {
                lblNote.Text = "Bạn nhập sai năm!";
                ltrDonwloadCount.Text = "";
                grdReportDownload.DataSource = null;
                grdReportDownload.DataBind();
                return;
            }

            int month = 1 ;
            int year = 2014;
            int.TryParse(txtMonth.Text.Trim(),out month);
            int.TryParse(txtYear.Text.Trim(), out year);

            if (month < 1 || month > 12)
            {
                lblNote.Text = "Bạn nhập sai tháng!";
                ltrDonwloadCount.Text = "";
                grdReportDownload.DataSource = null;
                grdReportDownload.DataBind();
                return;
            }

            DateTime expiry = new DateTime(year,month,1);
            BindData(expiry);
            
        }

        protected void btMonthNext_Click(object sender, EventArgs e)
        {
            lblNote.Text = "";
            int month = 1;
            int year = 2014;
            int.TryParse(txtMonth.Text.Trim(), out month);
            int.TryParse(txtYear.Text.Trim(), out year);
            
            if (month < 1 || month > 12)
            {
                lblNote.Text = "Bạn nhập sai tháng!";
                ltrDonwloadCount.Text = "";
                grdReportDownload.DataSource = null;
                grdReportDownload.DataBind();
                return;
            }
            month += 1;
            DateTime expiry = new DateTime();
            if (month >12)
            {
                month = 1;
                year += 1;

            }
            
            expiry = new DateTime(year, month, 1);

            txtMonth.Text = expiry.Month.ToString();
            txtYear.Text = expiry.Year.ToString();
            BindData(expiry);
        }

        protected void btMonthPre_Click(object sender, EventArgs e)
        {
            lblNote.Text = "";
            int month = 1;
            int year = 2014;
            int.TryParse(txtMonth.Text.Trim(), out month);
            int.TryParse(txtYear.Text.Trim(), out year);
            
            if (month < 1 || month > 12)
            {
                lblNote.Text = "Bạn nhập sai tháng!";
                ltrDonwloadCount.Text = "";
                grdReportDownload.DataSource = null;
                grdReportDownload.DataBind();
                return;
            }
            month -= 1;
            DateTime expiry = new DateTime();
            if (month <= 0)
            {
                month = 12;
                year -= 1;
                
            }
            expiry = new DateTime(year, month, 1);

            txtMonth.Text = expiry.Month.ToString();
            txtYear.Text = expiry.Year.ToString();
            BindData(expiry);
        }

        protected void grdReportDownload_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdReportDownload.PageIndex = e.NewPageIndex;
            int month = 1;
            int year = 2014;
            int.TryParse(txtMonth.Text.Trim(), out month);
            int.TryParse(txtYear.Text.Trim(), out year);

            if (month < 1 || month > 12)
            {
                lblNote.Text = "Bạn nhập sai tháng!";
                ltrDonwloadCount.Text = "";
                grdReportDownload.DataSource = null;
                grdReportDownload.DataBind();
                return;
            }

            DateTime expiry = new DateTime(year, month, 1);
            BindData(expiry);
            
        }
    }
}